package com.baigt.j8.lambda;

/**
 * Lambda表达式入门
 * https://docs.oracle.com/javase/8/
 * https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
 */
public class _001LambdaBegin {
    /**
     * Syntax of Lambda Expressions
     * A lambda expression consists of the following:
     * <p>
     * A comma-separated list of formal parameters enclosed in parentheses. The CheckPerson.test method contains one parameter, p, which represents an instance of the Person class.
     * <p>
     * Note: You can omit the data type of the parameters in a lambda expression. In addition, you can omit the parentheses if there is only one parameter. For example, the following lambda expression is also valid:
     * <p>
     * p -> p.getGender() == Person.Sex.MALE
     * && p.getAge() >= 18
     * && p.getAge() <= 25
     * The arrow token, ->
     * <p>
     * A body, which consists of a single expression or a statement block. This example uses the following expression:
     * <p>
     * p.getGender() == Person.Sex.MALE
     * && p.getAge() >= 18
     * && p.getAge() <= 25
     * If you specify a single expression, then the Java runtime evaluates the expression and then returns its value. Alternatively, you can use a return statement:
     * <p>
     * p -> {
     * return p.getGender() == Person.Sex.MALE
     * && p.getAge() >= 18
     * && p.getAge() <= 25;
     * }
     * A return statement is not an expression; in a lambda expression, you must enclose statements in braces ({}). However, you do not have to enclose a void method invocation in braces. For example, the following is a valid lambda expression:
     * <p>
     * email -> System.out.println(email)
     *
     * @param args
     */
    public static void main(String[] args) {
        new Thread(() -> {
        }, "thread-ricky").start();


        CheckPerson checkPerson = (p) -> 1 == 1;
        System.out.println(checkPerson.test(null));
        // Lambda表达式的构成
        // 入参(可以无参) +箭头标识+ 主体
        CheckPerson checkPersonAll = (Person p) -> {
            return true;
        };
    }
}

interface CheckPerson {
    boolean test(Person p);
}

class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}